<?php
/*
 * @Date: 2025-04-12 23:50:38
 * @LastEditors: 贾二小 erxiao.jia@outlook.com
 * @LastEditTime: 2025-04-12 23:55:27
 * @FilePath: /admin/app/Http/Controllers/RoleController.php
 */

namespace App\Http\Controllers;

use App\Http\Requests\StoreRoleRequest;
use App\Http\Requests\UpdateRoleRequest;
use App\Http\Resources\RoleResource;
use App\Models\Role;

class RoleController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $roles = Role::latest()
            ->key(request('key'))
            ->paginate(request('per_page', 15));

        return RoleResource::collection($roles);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreRoleRequest $request, Role $role)
    {
        $role->fill($request->input() + ['guard_name' => 'sanctum'])->save();

        return $this->success('添加成功', $role);
    }

    /**
     * Display the specified resource.
     */
    public function show(Role $role)
    {
        return $this->success(data: new RoleResource($role));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateRoleRequest $request, Role $role)
    {
        $role->fill($request->input())->save();

        return $this->success('更新成功', $role);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Role $role)
    {
        $role->delete();
        return $this->success(message: '删除成功');
    }
}
